package com.qq5194102.simpleskills.util;

import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.World;

/**
 * Created by 5194102 on 2025/7/18.
 * 特效工具类
 */
public class EffectUtil {

    /**
     * 特效枚举 对应playEffect()的参数、内容
     */
    public enum EffectType {
        FIREWORKS,   // 烟花效果
        HEARTS,      // 爱心效果
        CLOUD,       // 烟雾效果
        LIGHTNING,   // 闪电效果
        EXPLOSION,   // 爆炸效果
        RAINBOW      // 彩虹效果
    }

    /**
     * 播放特效
     * @param location 位置
     * @param effectType 特效类型
     */
    public static void playEffect(Location location, EffectType effectType) {
        World world = location.getWorld();
        if (world == null) return;

        switch (effectType) {
            case FIREWORKS:
                // 烟花粒子效果
                world.spawnParticle(Particle.FIREWORKS_SPARK, location, 50, 0.5, 0.5, 0.5, 0.5);
                world.spawnParticle(Particle.FLAME, location, 30, 0.5, 0.5, 0.5, 0.1);
                break;

            case HEARTS:
                // 爱心粒子效果
                world.spawnParticle(Particle.HEART, location, 15, 1, 1, 1);
                break;

            case CLOUD:
                // 烟雾效果
                world.spawnParticle(Particle.CLOUD, location, 50, 1, 1, 1, 0.3);
                world.spawnParticle(Particle.SMOKE_LARGE, location, 20, 1, 1, 1, 0.2);
                break;

            case LIGHTNING:
                // 闪电效果（视觉效果）
                world.strikeLightningEffect(location);
                break;

            case EXPLOSION:
                // 爆炸效果
                world.spawnParticle(Particle.EXPLOSION_HUGE, location, 1);
                world.spawnParticle(Particle.EXPLOSION_LARGE, location, 5);
                break;

            case RAINBOW:
                // 彩虹效果
                spawnRainbowExplosion(location);
                break;
        }
    }
    /**
     * 播放音效 TODO 可以改成不随机
     */
    public static void playSound(Location location) {
        World world = location.getWorld();
        if (world == null) return;

        // 随机选择一种音效
        Sound[] killSounds = {
                Sound.ENTITY_PLAYER_LEVELUP,
                Sound.ENTITY_EXPERIENCE_ORB_PICKUP,
                Sound.ENTITY_PLAYER_ATTACK_CRIT
        };

        Sound sound = killSounds[(int) (Math.random() * killSounds.length)];

        // 播放音效给附近所有玩家
        world.playSound(location, sound, 1.0f, 1.0f);
    }

    /**
     * 彩虹粒子特效
     */
    public static void spawnRainbowExplosion(Location location) {
        World world = location.getWorld();
        if (world == null) return;

        // 粒子类型数组
        Particle[] particles = {
                Particle.FLAME,
                Particle.FIREWORKS_SPARK,
                Particle.VILLAGER_HAPPY,
                Particle.VILLAGER_HAPPY,
                Particle.WATER_DROP,
                Particle.SPELL_WITCH
        };

        // 生成爆炸效果
        for (int i = 0; i < 100; i++) {
            // 随机选择粒子类型
            Particle particle = particles[(int)(Math.random() * particles.length)];

            // 随机方向
            double angle = Math.random() * Math.PI * 2;
            double radius = Math.random() * 2;
            double x = Math.cos(angle) * radius;
            double z = Math.sin(angle) * radius;
            double y = Math.random() * 2;

            Location particleLoc = location.clone().add(x, y, z);

            // 生成粒子
            world.spawnParticle(particle, particleLoc, 1);
        }
    }
}
